//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Reflection;

namespace Microsoft.Research.eResearch.Blackboard.Common
{
    /// <summary>
    /// This class reads the BB settings file BlackBoardPolicyFile.xml and
    /// stores the settings. These settings are then made available to 
    /// other consumers as method or properties.
    /// </summary>
    public class BlackBoardSettings
    {
        #region public Static Methods 

        /// <summary>
        /// Reads the settings from the policy file.
        /// </summary>
        public static void LoadSettings()
        {
            if (null != settings)
                return;
            ReadSettingsFile();
        }

        /// <summary>
        /// Reads the settings from the policy file from the path
        /// </summary>
        /// <param name="policyFilePath"></param>
        public static void LoadSettings(string policyFilePath)
        {
            if (null != settings)
                return;

            BlackBoardSettings.policyFilePath = policyFilePath;
            BlackBoardSettings.LoadSettings();
        }

        #endregion

        static BlackBoardSettings()
        {
            BlackBoardSettings.LoadSettings();
        }

        #region Public Methods

        /// <summary>
        /// Load the Xml File and then read settings.
        /// </summary>
        private static void ReadSettingsFile()
        {
            try
            {
                //Use the application path as path to search the policy file
                if (String.IsNullOrEmpty(policyFilePath))
                {
                    policyFilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                }
                
                settings = new XmlDocument();
                settings.Load(Path.Combine(policyFilePath, policyFileName));
            }
            catch (Exception ex)
            {
                string errorMessage = string.Format("Blackboard Policy file could not be loaded. -Exception:{0}", ex.Message);
                throw new PolicyException(errorMessage);
            }

            serializationSettings = new SerializationSettings();
            exceptionSettings = new ExceptionSettings();
            concepts = new MessageConcepts();
            blockedConcepts = new MessageBlockedConcepts();

            serializationSettings.ReadSettings(settings);
            exceptionSettings.ReadSettings(settings);
            concepts.ReadSettings(settings);
            blockedConcepts.ReadSettings(settings);
        }

        #endregion

        #region Public Properties

        public static SerializationSettings SerializationSettings
        { get { return serializationSettings; } }

        public static ExceptionSettings ExceptionSettings
        { get { return exceptionSettings; } }

        public static MessageConcepts Concepts
        { get { return concepts; } }

        public static MessageBlockedConcepts BlockedConcepts
        { get { return blockedConcepts; } }

        #endregion

        #region Private Data

        /// <summary>
        /// The XmlDocument object which represents the BlackBoardPolicyFile.xml.
        /// </summary>
        private static XmlDocument settings = null;

        /// <summary>
        /// Path to the policy file. Default is the current directory.
        /// </summary>
        private static string policyFilePath = string.Empty;

        /// <summary>
        /// Object for storing the serializtion settings
        /// </summary>
        private static SerializationSettings serializationSettings;

        /// <summary>
        /// Object for storing the exception settings
        /// </summary>
        private static ExceptionSettings exceptionSettings;

        /// <summary>
        /// Object for storing the message concepts
        /// </summary>
        private static MessageConcepts concepts;

        /// <summary>
        /// Object for storing the message blocked concepts.
        /// </summary>
        private static MessageBlockedConcepts blockedConcepts;
             
        /// <summary>
        /// Name of the policy file
        /// </summary>
        private const string policyFileName = "BlackBoardPolicyFile.xml";

        #endregion
    }

}
